
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using GruntXProductions;
using Viper;
using Viper.Opcodes;

namespace Diamond
{
	public partial class CodeGen 
	{
		public void GetStringValue(SyntaxNode sn)
		{
			if (sn is SymbolRef)
			{
				Assembler.Emit(new push_ptr(Symbols[((SymbolRef)sn).Value]));
				Assembler.Emit(new dload());
			}
			else if (sn is StringLiteral)
			{
				Assembler.Emit(new push_ptr(Symbols.CreateTempString(((StringLiteral)sn).Value)));
			}
			else if (sn is FunctionCall)
			{
				FunctionCall fc = sn as FunctionCall;
				if(GetReturnType(fc) != "string")
					Errors.Add("Method " + fc.Target + " does not return type string!");
				else
					Call(fc);
			}
			else if (sn is Expression)
			{
				ParseStringExpression(sn as Expression);
			}
		}
		public void ParseStringExpression(Expression exp)
		{
			foreach(SyntaxNode sn in exp.Items)
			{
				if(sn is Operator)
				{
					Operator op = sn as Operator;
					if(isOverloaded(op, "string"))
					{
						CallOperator(op, getOverload(op, "string"));
					}
					
					else 
					{
						GetStringValue(op.Op1);
						GetStringValue(op.Op2);
						if(sn is Plus)
							Assembler.Emit(new sysf (3));
						else
						{
							Errors.Add("Operator '" + op.theOperator + "' can not be applied between string!");
						}
					}
				}
				else
				{
					GetStringValue(sn);
				}
			}
		}
	}
	
}